package com.example.module_motorcircle.post.viewmodel

import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.state.UIState
import com.example.lib_net.model.api.PostItem
import com.example.module_motorcircle.motor_circle.repository.MotorCircleRepository
import com.example.module_motorcircle.post.repository.PostRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/13
 */
@HiltViewModel
class PostViewModel @Inject constructor(private val repository: PostRepository) :BaseViewModel(){
    /**
     * 获得帖子数据
     */
    private val postState = MutableStateFlow<UIState>(UIState.Default)
    val postState_ = postState.asStateFlow()

    var postList:List<PostItem> = listOf()
    fun getPostPage(category: Int? = 0,pageNum: Int = 1,pageSize: Int = 10){
        execute(
            action = repository.getPostPage(category,pageNum,pageSize),
            onFinished = {
                if(it.data.records.isNullOrEmpty()){//空数据
                    postState.value = UIState.Empty
                }else{
                    postList = it.data.records!! //解决数据不显示问题
                    postState.value = UIState.Success
                }
            },
            onStart = {
                postState.value = UIState.Loading
            },
            onError = {
                postState.value = UIState.Error(it)
            }
        )
    }
    /**
     * 帖子点赞和取消
     */
    private val requestLikeEvent = MutableSharedFlow<Int>() //下标
    val requestLikeEvent_ = requestLikeEvent.asSharedFlow()
    fun likeOrCancel(postId:String,position:Int){
        execute(
            action = repository.likeOrCancel(postId),
            onError = {
                requestLikeEvent.emit(-1)
            },
            onFinished = {
                requestLikeEvent.emit(position)
            }
        )
    }

    /**
     * 帖子收藏和取消
     */
    private val requestCollectEvent = MutableSharedFlow<Int>() //下标
    val requestCollectEvent_ = requestCollectEvent.asSharedFlow()
    fun collectOrCancel(ifCollect:Int,postId:String,position:Int){
        execute(
            action = repository.collectOrCancel(ifCollect,postId),
            onError = {
                requestCollectEvent.emit(-1)
            },
            onFinished = {
                requestCollectEvent.emit(position)
            }
        )
    }
    /**
     * 关注
     */
    private val requestFollowEvent = MutableSharedFlow<Int>() //下标
    val requestFollowEvent_ = requestFollowEvent.asSharedFlow()
    fun followMember(memberId:String,position: Int){
        execute(
            action = repository.followMember(memberId),
            onError = {
                requestFollowEvent.emit(-1)
            },
            onFinished = {
                requestFollowEvent.emit(position)
            }
        )
    }

    /**
     * 取消关注
     */
    fun followCancelMember(memberId:String,position: Int){
        execute(
            action = repository.followCancelMember(memberId),
            onError = {
                requestFollowEvent.emit(-1)
            },
            onFinished = {
                requestFollowEvent.emit(position)
            }
        )
    }
    /**
     * 举报帖子
     */
    private val requestShieldEvent = MutableSharedFlow<Boolean>()
    val requestShieldEvent_ = requestShieldEvent.asSharedFlow()
    fun shieldSave(id:String){
        execute(
            action = repository.shieldSave(id,1),
            onError = {
                requestShieldEvent.emit(false)
            },
            onFinished = {
                requestShieldEvent.emit(true)
            }
        )
    }
    /**
     * 删除帖子
     */
    private val requestDeletePostEvent = MutableSharedFlow<Boolean>()
    val requestDeletePostEvent_ = requestDeletePostEvent.asSharedFlow()
    fun deletePost(id:String){
        execute(
            action = repository.deletePost(id),
            onError = {
                requestDeletePostEvent.emit(false)
            },
            onFinished = {
                requestDeletePostEvent.emit(true)
            }
        )
    }
}